import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HelloController } from './hello/hello.controller';
import { HelloService } from './hello/hello.service';
import { HelloModule } from './hello/hello.module';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import envConfig from '../config/env';
import { HelloEntity } from './hello/hello.entity';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 设置为全局
      envFilePath: [envConfig.path],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],

      useFactory: async (configService: ConfigService) => {
        console.log(
          '%c [ configService.get ] ==> 23 - app.module.ts',
          'font-size:13px; background:pink; color:#bf2c9f;',
          configService.get('DB_HOST', 'localhost'),
          configService.get<number>('DB_PORT', 3306),
          configService.get('DB_USER', 'root'),
          configService.get('DB_PASSWD', 'root'),
          configService.get('DB_DATABASE', 'blog'),
        );
        return {
          type: 'mysql', // 数据库类型
          entities: [HelloEntity], // 数据表实体
          autoLoadEntities: true,
          host: configService.get('DB_HOST', 'localhost'), // 主机，默认为localhost
          port: configService.get<number>('DB_PORT', 3306), // 端口号
          username: configService.get('DB_USER', 'root'), // 用户名
          password: configService.get('DB_PASSWD', 'root'), // 密码
          database: configService.get('DB_DATABASE', 'blog'), //数据库名
          // host: 'localhost', // 主机，默认为localhost
          // port: 3306, // 端口号
          // username: 'root', // 用户名
          // password: 'root', // 密码
          // database: 'sb_demo', //数据库名
          timezone: '+08:00', //服务器上配置的时区
          synchronize: false, //根据实体自动创建数据库表， 生产环境建议关闭
        };
      },
    }),
    HelloModule,
    TypeOrmModule.forFeature([HelloEntity]),
  ],
  controllers: [AppController, HelloController],
  providers: [AppService, HelloService],
})
export class AppModule {}
